package com.cb.web.wasf.sample.web;

import com.cb.web.wasf.sample.web.Controller;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Singleton;
import com.google.inject.persist.PersistFilter;
import com.google.inject.persist.jpa.JpaPersistModule;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.servlet.ServletModule;
import java.lang.ref.WeakReference;
import org.h2.server.web.WebServlet;

/**
 * @author CBO
 */
public class WasfApplicationListener extends GuiceServletContextListener {

    @Override
    protected Injector getInjector() {
        Injector injector = Guice.createInjector(new ServletModule() {

            @Override
            protected void configureServlets() {
                install(new JpaPersistModule("dbUnit"));
                filter("/*").through(PersistFilter.class);
                serve("/controller").with(Controller.class);
                bind(WebServlet.class).in(Singleton.class);
                serve("/h2").with(WebServlet.class);
            }
        });
        ManagerLocator.injector = new WeakReference<Injector>(injector);
        return injector;
    }
}
